package cn.magic0610.realtime;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class TimeSyncer extends BukkitRunnable {
    @Override
    public void run() {
        GregorianCalendar calender = new GregorianCalendar();
        long timeTick = ((calender.get(Calendar.HOUR_OF_DAY) - 6 ) * 1000) + (calender.get(Calendar.MINUTE) * 16);
        if (timeTick < 0) {
            timeTick = 24000 + timeTick;
        }
        for (World world : Bukkit.getWorlds()) {
            if (Functions.config.getStringList("sync-time.disable-worlds").contains(world.getName())) {
                continue;
            }
            long betweenTick = timeTick - world.getTime();
            long setTick;
            if (betweenTick >= 0) {
                setTick = world.getFullTime() + betweenTick;
            } else {
                setTick = world.getFullTime() + (24000 + betweenTick);
            }
            world.setFullTime(setTick);
        }
    }
}
